import Vue from 'vue'
import Vuex from 'vuex'
import axios from '@/utils/axios'
import apis from '../apis'
Vue.use(Vuex);
const toTree = (data)=>{
    var rs = {};
    for(let i=0;i<data.length;i++){
        var permission = data[i];
    // 如果parentId是0,则表示它是树的第一级元素
        // console.log(permission);
        if(permission.parentid==0){
            // rs.push(permission);
            permission.children = [];
            rs[permission._id] = permission;
        }        
    }
    for(let i=0;i<data.length;i++){
        var permission = data[i];
        for(var key in rs){
            if(permission.parentid == key){
                rs[key].children.push(permission)
            }
        }
    }
    console.log("结果");
    console.log(rs);
    return rs;
}
	const state ={
        allPermissions: [],
        allRoles: [],
        allUsers: [], //所有账号
        allMenus: [],
        allGames: [],//所有彩种
        currentRoles: [],//当前用户的角色组
        defaultActive: '0-0', //导航条默认激活的选项
    }
    const getters={
        allMenus(state){
            return toTree(state.allMenus);
        },
        allPermissions(state){
            return toTree(state.allPermissions);
        },
        allUsers(state){
            return state.allUsers;
        },
        allRoles(state){
            return state.allRoles;
        },
        allGames(state){
            return state.allGames;
        },
        currentRoles(state){
            if(state.currentRoles.length){
                return state.currentRoles;
            }else{
                if(localStorage.getItem("response")){
                    return JSON.parse(localStorage.getItem("response")).roles;
                }else{
                    return []
                }
            }
        }
    }
    const mutations={
        LOADALLPERMISSIONS(state,payload){
            state.allPermissions = payload;
        },
        LOADALLROLES(state,payload){
            state.allRoles = payload;
        },
        LOADALLUSERS(state,payload){
            state.allUsers = payload;
        },
        LOADALLMENUS(state,payload){
            state.allMenus = payload;
        },
        LOADCURRENTROLES(state,payload){
            state.currentRoles = payload;
        },
          // 修改默认展开的侧边栏项
        CHANGEDEFAULTACTIVE(state,payload){
            state.defaultActive = payload;
        },
        LOADALLGAMES(state,payload){
            state.allGames = payload;
        }
    }
    const actions={
        loadAllRole({commit},payload){
            // 加载所有的角色
            axios.get(apis.findAllRoles)
            .then((resp)=>{
                commit('LOADALLROLES',resp.data.allRoles)
            })
        },
         // 获取所有权限
	    loadAllPermission({commit},payload){
	        // 发起请求,加载所有的权限
	        axios.get(apis.findAllPermission)
	        .then((resp)=>{
	            // 提交到mutations里更改
	           commit('LOADALLPERMISSIONS',resp.data.allPermission)  
	        })
	    },
        loadAllGames({commit},payload){
            axios.get(apis.findAllGames)
            .then((resp)=>{
                commit("LOADALLGAMES",resp.data.data[0].games);
                console.log(resp.data.data[0].games)
            })
        }
    }
export default {
	getters,
    state,
    mutations,
    actions
}
